import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.utils import ForumDriver


class ForumHome:
    def __init__(self):
        self.driver=ForumDriver.driver
        # 确保页面加载完成
        WebDriverWait(self.driver, 5).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="article_list_board_title"]'))
        )

    def HomeTest(self):
        # 1.验证首页页面的元素是否存在：论坛图片，模块（C++,Java）
        elements=['/html/body/div[1]/header[1]/div/h1/a/img',
                  '//*[@id="topBoardList"]/li[2]/a/span[2]',
                  '//*[@id="topBoardList"]/li[3]/a/span[2]']
        for e in elements:
            WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.XPATH, e))
            )
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 2.测试点击不同的模块，是否会展示对应模块的帖子
        self.driver.find_element(By.XPATH,'//*[@id="topBoardList"]/li[3]/a/span[2]').click()
        WebDriverWait(self.driver, 5).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="article_list_board_title"]'))
        )
        text = self.driver.find_element(By.XPATH,'//*[@id="article_list_board_title"]').text
        assert text=="C++"
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)
        # 返回首页
        self.driver.find_element(By.XPATH, '//*[@id="nav_board_index"]/a/span[2]').click()

        # 3.测试点击“发表帖子”按钮进入发表帖子页面后，是否存在字段“发新贴”
        self.driver.find_element(By.XPATH, '//*[@id="bit-forum-content"]/div[1]/div/div/div[2]/div/a[1]').click()
        WebDriverWait(self.driver, 5).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="bit-forum-content"]/div[1]/div/div/div/h2'))
        )
        text = self.driver.find_element(By.XPATH, '//*[@id="bit-forum-content"]/div[1]/div/div/div/h2').text
        assert text == "发新贴"
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 返回首页
        self.driver.find_element(By.XPATH, '//*[@id="nav_board_index"]/a/span[2]').click()
        # 4.验证帖子列表是否展示贴子的标题、发布者、发布者头像、总点击量、被点赞数和评论数
        elements = ['//*[@id="artical-items-body"]/div[1]/div/div[2]/div[1]',
                    '//*[@id="artical-items-body"]/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[1]',
                    '//*[@id="artical-items-body"]/div[1]/div/div[1]/span',
                    '//*[@id="artical-items-body"]/div[1]/div/div[2]/div[2]/div/div[2]/ul/li[1]',
                    '//*[@id="artical-items-body"]/div[1]/div/div[2]/div[2]/div/div[2]/ul/li[2]',
                    '//*[@id="artical-items-body"]/div[1]/div/div[2]/div[2]/div/div[2]/ul/li[3]',
                    ]
        for e in elements:
            WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.XPATH, e))
            )
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 5.测试点击右上角的月亮/太阳标志，能切换夜间/白天模式
        self.driver.find_element(By.XPATH,'/html/body/div[1]/header[1]/div/div/div[2]/a[1]').click()
        curUrl=self.driver.current_url
        assert curUrl=="http://127.0.0.1:58080/index.html?theme=dark"
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        self.driver.find_element(By.XPATH, '/html/body/div[1]/header[1]/div/div/div[2]/a[2]').click()
        curUrl = self.driver.current_url
        assert curUrl == "http://127.0.0.1:58080/index.html?theme=light"
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 6.展示登录用户的头像和昵称，点击头像和名称区域可看见3个选项：我的帖子，个人中心，退出
        self.driver.find_element(By.XPATH,'/html/body/div[1]/header[1]/div/div/div[3]').click()
        elements=['//*[@id="index_user_profile"]',
                  '//*[@id="index_user_settings"]',
                  '//*[@id="index_user_logout"]']
        for e in elements:
            WebDriverWait(self.driver,3).until(
                EC.presence_of_element_located((By.XPATH,e))
            )
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 点击我的帖子，进入“我的帖子”页面,测试是否存在昵称和个人介绍
        self.driver.find_element(By.XPATH,'//*[@id="index_user_profile"]').click()
        elements = ['// *[ @ id = "profile_nickname"]',
                    '//*[@id="bit-forum-content"]/div[2]/div/div/div[2]/div/div/div/div/h2']
        for e in elements:
            WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.XPATH, e))
            )
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 再次点击头像
        self.driver.find_element(By.XPATH,'/html/body/div[1]/header[1]/div/div/div[3]').click()
        # 点击个人中心，进入“用户中心”页面
        self.driver.find_element(By.XPATH,'//*[@id="index_user_settings"]').click()
        text=self.driver.find_element(By.XPATH,'//*[@id="bit-forum-content"]/div[1]/div/div/div/h2').text
        assert text=="用户中心"
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 再次点击头像
        self.driver.find_element(By.XPATH, '/html/body/div[1]/header[1]/div/div/div[3]').click()
        # 点击退出，返回登录页面
        self.driver.find_element(By.XPATH,'//*[@id="index_user_logout"]').click()
        time.sleep(1)
        curUrl=self.driver.current_url
        assert curUrl=="http://127.0.0.1:58080/sign-in.html"
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)




# ForumHome().HomeTest()








